﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _5._3._2_NorthWind_Task
{
    public partial class DetailedView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                List<Employee> list = new List<Employee>();
                list.Add(this.LoadEmployee());
                this.DetailsViewEmployee.DataSource = list;
                this.DetailsViewEmployee.DataBind();

                this.FormViewEmployee.DataSource = list;
                this.FormViewEmployee.DataBind();

                this.EmployeesRepeater.DataSource = list;
                this.EmployeesRepeater.DataBind();

                this.EmployeesListView.DataSource = list;
                this.EmployeesListView.DataBind();
            }
        }

        private Employee LoadEmployee()
        {
            Employee employee;

            int employeeID;
            bool result = int.TryParse(Request.Params["EmployeeID"], out employeeID);
            if (!result)
            {
                Response.Redirect("StartPage.aspx");
            }

            NorthwindEntities context = new NorthwindEntities();

            employee = context.Employees.FirstOrDefault(e => e.EmployeeID == employeeID);
            return employee;
        }

        public void Button_Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("StartPage.aspx");
        }
    }
}